﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoDecision
    {
        private int _nbTicksPassed = -1;
        private float _mineralsMinedTotal = -1.0f;
        private float _gasMinedTotal = -1.0f;
        private float _mineralsAvailable = -1.0f;
        private float _gasAvailable = -1.0f;
        private int _nbLarvaeAvailable = -1;
        private float _supplyUsed = -1;
        private float _supplyAvailable = -1;
        private BoCommand _command = null;

        public BoDecision(BoDecisionNode n)
        {
            _nbTicksPassed = n.gameState.nbTicksPassed;
            _mineralsMinedTotal = n.gameState.mineralsMinedTotal;
            _gasMinedTotal = n.gameState.gasMinedTotal; ;
            _mineralsAvailable = n.gameState.mineralsAvailable;
            _gasAvailable = n.gameState.gasAvailable;

            _nbLarvaeAvailable = n.gameState.nbLarvaeAvailable;
            _supplyUsed = n.gameState.supplyUsed;
            _supplyAvailable = n.gameState.supplyAvailable;

            _command = (!n.hasChildren()) ? null : n.leftChild.command;
        }
        private void convertTicksToTime(int nbTicks, out int minutes, out int seconds)
        {
            minutes = nbTicks / 60;
            seconds = nbTicks - (minutes * 60);
        }
        public string toString()
        {
            int minutes = 0;
            int seconds = 0;
            convertTicksToTime(_nbTicksPassed, out minutes, out seconds);

            string minutesStr = minutes.ToString();
            string secondsStr = seconds.ToString();
            minutesStr = (minutes <= 9) ? ("0" + minutesStr) : minutesStr;
            secondsStr = (seconds <= 9) ? ("0" + secondsStr) : secondsStr;

            string timeStr = minutesStr + ":" + secondsStr;
            string minStr = string.Format("{0:0}M", _mineralsAvailable);
            string gasStr = string.Format("{0:0}G", _gasAvailable);
            string nbLarvaeStr = string.Format("{0}L", _nbLarvaeAvailable);
            string supplyStr = _supplyUsed + "/ " + _supplyAvailable + "S";

            string str;
            if (_command != null)
            {
                str = string.Format("{0, 0}   {1, 6}   {2, 4}   {3, 4}   {4, 8}    {5}", timeStr, minStr, gasStr, nbLarvaeStr, supplyStr, _command.toString());
            }
            else
            {
                str = string.Format("{0, 0}   {1, 6}   {2, 4}   {3, 4}   {4, 8}    {5}", timeStr, minStr, gasStr, nbLarvaeStr, supplyStr, "finished");
            }
            return str;
        }

        public float mineralsMinedTotal
        {
            get
            {
                return _mineralsMinedTotal;
            }
        }
        public float gasMinedTotal
        {
            get
            {
                return _gasMinedTotal;
            }
        }
        public int nbTicksPassed
        {
            get
            {
                return _nbTicksPassed;
            }
        }
    }
}
